package in.muton.edushare.panels.images;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.url.URLLoader;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class ImageWithLink extends Panel {

	private static final long serialVersionUID = 8514592772625701216L;
	private IModel<? extends IFileUploadable> model;
	private String imageVersion;

	public ImageWithLink(String id, IModel<? extends IFileUploadable> model, String imageVersion) {
		super(id, model);
		this.model = model;
		this.imageVersion = imageVersion;
		loadPanel();
	}

	private void loadPanel() {
		Class<? extends WebPage> homePageClass = null;
		boolean isLoggedIn = EduShareSession.get().isLoggedIn();
		if(model.getObject() instanceof Universities){
			if(isLoggedIn)
				homePageClass = URLLoader.getPageClass(URLConstants.UNIVERSITY_PROFILE_PATH);
			else
				homePageClass = URLLoader.getPageClass(URLConstants.UNIV_PUBLIC_PAGE_HOME);
		}
		else if(model.getObject() instanceof Departments){
			if(!isLoggedIn)
				homePageClass = URLLoader.getPageClass(URLConstants.DEPT_PUBLIC_PAGE_HOME);
			else
				homePageClass = URLLoader.getPageClass(URLConstants.DEPARTMENT_PROFILE_PATH);
		}
		else if(model.getObject() instanceof Courses || model.getObject() instanceof CourseInstances){
			if(!isLoggedIn)
				homePageClass = URLLoader.getPageClass(URLConstants.COURSE_PUBLIC_PAGE_HOME);
			else
				homePageClass = URLLoader.getPageClass(URLConstants.COURSE_PROFILE_PATH);
		}else if(model.getObject() instanceof Users){
			if(!isLoggedIn)
				homePageClass = URLLoader.getPageClass(URLConstants.USER_PUBLIC_PAGE_SUMMARY);
			else
				homePageClass = URLLoader.getPageClass(URLConstants.USER_PROFILE_PATH);
		}else if(model.getObject() instanceof Groups){
			homePageClass = URLLoader.getPageClass(URLConstants.GROUPS_NEWS);
		}else{
			throw new EduShareRuntimeException("Illegal Entity.");
		}
		
		BookmarkablePageLink<Void> homePageLink = new BookmarkablePageLink<Void>
														(
														"homePageLink", 
														homePageClass, 
														EntityPageParams.getParams(this.model)
														);
		add(homePageLink);
		homePageLink.add(new ProfileImage("profileImg", this.model, this.imageVersion));
	}

}
